<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id_user
 * @property string $username
 * @property string $password
 * @property string $email
 * @property boolean $trangthai
 * @property string $diachi
 * @property boolean $gioitinh
 * @property string $avatar
 */
class User extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
    
        const TT_NAM = 1;
        const TT_NU = 0;
        public $password_repeat;
        
        public function getGioitinh()
        {
            return array(
            self::TT_NAM=>'Nam',
            self::TT_NU=>'Nữ'
            );
        }
        
        public function getTrangthai()
        {
            return array(
            self::TT_NAM=>'Hiện',
            self::TT_NU=>'Ẩn',
            );
        }
        public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password,password_repeat, email, trangthai', 'required'),
                        array('avatar', 'file', 'allowEmpty'=>true,'types'=>'jpg,gif,png','on'=>'update'),
                        array('password','compare','compareAttribute'=>'password_repeat'),
                        array('avatar','file','types'=>'jpg,gif,png','allowEmpty'=>true),
			array('username', 'length', 'max'=>50),
			array('password, email', 'length', 'max'=>100),
			array('diachi', 'length', 'max'=>250),
			array('gioitinh, avatar', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id_user, username, email, trangthai, diachi, gioitinh', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
                        'password_repeat'=>'Nhập lại mật khẩu',
			'id_user' => 'ID',
			'username' => 'Tài Khoản',
			'password' => 'Mật Khẩu',
			'email' => 'Email',
			'trangthai' => 'Trạng Thái',
			'diachi' => 'Địa Chỉ',
			'gioitinh' => 'Giới Tính',
			'avatar' => 'Avatar',
		);
	}
        
        public function hashPassword($password)
        {
            return md5($password);
        }
        
        public function validatePassword($password)
        {
            return $this->hashPassword($password)===$this->password;
        }
        
        public function afterValidate() {
            parent::afterValidate();
            if (!$this->hasErrors()) {
                $this->password = $this->hashPassword($this->password);
            }
        }
        
        
        
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_user',$this->id_user);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('trangthai',$this->trangthai);
		$criteria->compare('diachi',$this->diachi,true);
		$criteria->compare('gioitinh',$this->gioitinh);
		$criteria->compare('avatar',$this->avatar,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
